package com.shop.cereshop.app.param.wallet;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.shop.cereshop.commons.utils.RsaUtils;
import com.shop.cereshop.commons.config.RsaProperties;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.math.BigDecimal;

/**
 * 钱包支付执行请求参数
 */
@Data
@ApiModel("钱包支付执行请求参数")
public class WalletPayExecuteRequest {

    @ApiModelProperty(value = "订单ID", required = true, example = "12345")
    @NotNull(message = "订单ID不能为空")
    private Long orderId;

    @ApiModelProperty(value = "订单类型 1-父订单 2-子订单", required = true, example = "2")
    @NotNull(message = "订单类型不能为空")
    private Integer type;

    @ApiModelProperty(value = "拼单id")
    private Long collageId;

    @ApiModelProperty(value = "加密后的支付密码", required = true, example = "encrypted_password_string")
    @NotBlank(message = "支付密码不能为空")
    private String password;

    /**
     * 验证支付密码格式是否正确
     */
    @JsonIgnore
    public boolean isPasswordValidFormat() {
        try {
            if (password == null || password.trim().isEmpty()) {
                return false;
            }
            // 尝试解密支付密码
            String decryptedPassword = RsaUtils.decryptByPrivateKey(RsaProperties.privateKey, password);
            return decryptedPassword != null && decryptedPassword.length() == 6;
        } catch (Exception e) {
            return false;
        }
    }

    /**
     * 获取解密后的支付密码
     */
    @JsonIgnore
    public String getDecryptedPassword() {
        try {
            return RsaUtils.decryptByPrivateKey(RsaProperties.privateKey, password);
        } catch (Exception e) {
            return null;
        }
    }
}
